package com.zgc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zgc.common.R;
import com.zgc.entity.Category;
import com.zgc.entity.Employee;
import com.zgc.entity.Permission;
import com.zgc.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @GetMapping("/all")
    public R<List> getAllPermissions(){
        //log.info("权限开始查询。。。。。。。");
        List<Permission> list= permissionService.list();
        return R.success(list);
    }

    @GetMapping("/page")
    public R<Page> getAllPagePermissions(int page,int pageSize,String name){
        log.info("权限分页查询。。。。。。。");
        Page<Permission> page1 = new Page<>(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Permission::getName,name);
        permissionService.page(page1,queryWrapper);
        return R.success(page1);
    }

    @DeleteMapping
    public R<String> delete(Long id){
        log.info("删除分类，id为：{}",id);
        permissionService.removeById(id);
        return R.success("分类信息删除成功");
    }

    @PostMapping
    public R<String> save(@RequestBody Permission permission){
        log.info("category:{}",permission);
        permissionService.save(permission);
        return R.success("新增分类成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Permission permission){
        log.info("修改分类信息：{}",permission);
        permissionService.updateById(permission);
        return R.success("修改分类信息成功");
    }

}
